package com.kevin.swing;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.kevin.MainFrame;
import com.kevin.swing.impl.AboutDialog;
import com.kevin.swing.impl.ComWordsSetDialog;
import com.kevin.swing.impl.DBSetDialog;
import com.kevin.swing.impl.HelpDialog;
import com.kevin.swing.impl.WordSetDialog;

/**
 * 菜单栏
 * 
 * @ClassName MenuBar
 * @author <a href="mailto:donggongai@126.com" target="_blank">kevin</a>
 * @date 2016年8月26日 下午2:28:32
 *
 */
public class MenuBar extends JMenuBar {
    /**
     * @Fields serialVersionUID
     */
    private static final long serialVersionUID = 3688209859562875772L;

    private MainFrame owner;

    // 菜单栏
    private JMenu sysMenu = new JMenu("系统");
    private JMenuItem dbItem = new JMenuItem("数据库设置");
    private JMenuItem wordSetItem = new JMenuItem("分词设置");
    private JMenuItem comWordsSetItem = new JMenuItem("常用词设置");
    private JMenuItem exitItem = new JMenuItem("退出");
    private JMenu aboutMenu = new JMenu("帮助");
    private JMenuItem helpItem = new JMenuItem("使用帮助");
    private JMenuItem aboutItem = new JMenuItem("关于本软件");

    // 设置
    private DBSetDialog dbSetPanel;
    private ComWordsSetDialog comWordsSetPanel;
    private WordSetDialog wordSetPanel;
    private HelpDialog helpPanel;
    private AboutDialog aboutPanel;

    public MenuBar(MainFrame owner) {
        super();
        this.owner = owner;
        setMainBar();
    }

    private void setMainBar() {
        this.add(sysMenu);
        this.add(aboutMenu);
        sysMenu.add(dbItem);
        sysMenu.addSeparator();
        sysMenu.add(comWordsSetItem);
        sysMenu.add(wordSetItem);
        sysMenu.addSeparator();
        sysMenu.add(exitItem);
        aboutMenu.add(helpItem);
        aboutMenu.add(aboutItem);

        dbItem.addActionListener(e -> {
            if (dbSetPanel == null) {
                dbSetPanel = new DBSetDialog(owner);
                dbSetPanel.setLocationRelativeTo(owner);
            }
            dbSetPanel.setVisible(true);
        });

        comWordsSetItem.addActionListener(e -> {
            if (comWordsSetPanel == null) {
                comWordsSetPanel = new ComWordsSetDialog(owner);
                comWordsSetPanel.setLocationRelativeTo(owner);
            }
            comWordsSetPanel.setVisible(true);
        });

        wordSetItem.addActionListener(e -> {
            if (wordSetPanel == null) {
                wordSetPanel = new WordSetDialog(owner);
                wordSetPanel.setLocationRelativeTo(owner);
            }
            wordSetPanel.setVisible(true);
        });

        // 退出
        exitItem.addActionListener(e -> {
            owner.exitSoft();
        });

        helpItem.addActionListener(e -> {
            if (helpPanel == null) {
                helpPanel = new HelpDialog(owner);
                helpPanel.setLocationRelativeTo(owner);
            }
            helpPanel.setVisible(true);
        });
        // 关于
        aboutItem.addActionListener(e -> {
            if (aboutPanel == null) {
                aboutPanel = new AboutDialog(owner);
                aboutPanel.setLocationRelativeTo(owner);
            }
            aboutPanel.setVisible(true);
        });

    }

}
